package com.hlj.ncp.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlj.ncp.business.entity.Merchant;
import com.hlj.ncp.business.service.IMerchantService;
import com.hlj.ncp.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 商户管理 控制器
 */
@Controller
@RequestMapping("/business/merchant")
public class MerchantController {
    
    @Autowired
    private IMerchantService merchantService;
    
    /**
     * 获取商户列表
     */
    @ResponseBody
    @GetMapping("/list")
    public AjaxResult list(Merchant merchant, 
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 验证分页参数
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        Page<Merchant> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        
        // 设置查询条件
        if (merchant.getMerchantName() != null && !merchant.getMerchantName().isEmpty()) {
            queryWrapper.like(Merchant::getMerchantName, merchant.getMerchantName());
        }
        if (merchant.getMerchantType() != null && !merchant.getMerchantType().isEmpty()) {
            queryWrapper.eq(Merchant::getMerchantType, merchant.getMerchantType());
        }
        if (merchant.getStatus() != null && !merchant.getStatus().isEmpty()) {
            queryWrapper.eq(Merchant::getStatus, merchant.getStatus());
        }
        
        queryWrapper.orderByDesc(Merchant::getCreateTime);
        
        Page<Merchant> resultPage = merchantService.page(page, queryWrapper);
        return AjaxResult.success(resultPage);
    }
    
    /**
     * 获取商户详细信息
     */
    @ResponseBody
    @GetMapping("/{merchantId}")
    public AjaxResult getInfo(@PathVariable Long merchantId) {
        return AjaxResult.success(merchantService.getById(merchantId));
    }
    
    /**
     * 新增商户
     */
    @ResponseBody
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant) {
        return merchantService.save(merchant) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 修改商户
     */
    @ResponseBody
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant) {
        return merchantService.updateById(merchant) ? AjaxResult.success() : AjaxResult.error();
    }
    
    /**
     * 删除商户
     */
    @ResponseBody
    @DeleteMapping("/{merchantIds}")
    public AjaxResult remove(@PathVariable Long[] merchantIds) {
        for (Long merchantId : merchantIds) {
            merchantService.removeById(merchantId);
        }
        return AjaxResult.success();
    }

    /**
     * 返回商户管理页面
     */
    @GetMapping("")
    public String index() {
        return "business/merchant/index";
    }
} 